<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>服务管理</el-breadcrumb-item>
      <el-breadcrumb-item>服务列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索查询 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <div>
            <el-input v-model="queryParam.title" placeholder="请输入标题" class="input-with-select" clearable></el-input>
          </div>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="handleSizeChange(this.queryParam)">查询</el-button>
          <el-button type="primary" @click="adornProcessFormVisible = true">添加装修流程</el-button>
        </el-col>
      </el-row>
      <!-- table表格数据 start -->
      <template>
        <el-table :data="adornProcessDataList" style="width: 100%" max-height="570">
          <!-- 数据表 -->
          <el-table-column fixed prop="title" label="标题" width="100">
          </el-table-column>
          <el-table-column prop="icon" label="图标" width="90">
            <template slot-scope="scope">
              <span class="icon1 iconfont" :class="scope.row.icon"></span>
            </template>
          </el-table-column>
          <el-table-column prop="notes" label="备注" width="120">
          </el-table-column>
          <el-table-column prop="describe" label="描述" width="350">
          </el-table-column>
          <el-table-column prop="thumbnail" label="背景图" width="120">
            <template slot-scope="scope">
              <div class="demo-image__preview">
                <el-image style="width: 100px; height: 100px" :src="scope.row.thumbnail_path" :preview-src-list="[scope.row.thumbnail_path]">
                </el-image>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="related_articles" label="关联文章" width="80">
            <template slot-scope="scope">
              <i @click="relateArticle(scope.row)" style="font-size: 30px;color: blue;" class="el-icon-link"></i>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="140">
            <!-- 操作 -->
            <template slot-scope="scope">
              <el-button size="mini" type="warning" icon="el-icon-view" circle></el-button>
              <el-button size="mini" type="warning" icon="el-icon-edit" @click="editAdornProcess(scope.row)" circle></el-button>
              <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteAdornProcess(scope.$index,scope.row.id)"
                circle></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- table表格数据 end -->
      <!-- 分页  start-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryParam.currentPage"
        :page-sizes="[10, 20, 30, 40]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="totalResult">
      </el-pagination>
      <!-- 分页  end> -->
    </el-card>
    <!-- 表单 start      :rules="adornProcessFormRules"                     -->
    <el-dialog title="装修流程" :visible.sync="adornProcessFormVisible" @close="closeAdornProcessForm" width="30%" center>
      <el-form :model="adornProcessForm" ref="adornProcessFormRef" label-width="120px" class="demo-ruleForm">
        <el-form-item label="装修流程" required prop="title">
          <el-input v-model="adornProcessForm.title"></el-input>
        </el-form-item>
        <el-form-item label="装修流程图标" required prop="icon">
          <el-input v-model="adornProcessForm.icon"></el-input>
        </el-form-item>
        <el-form-item label="装修流程颜色" required prop="bg_color">
          <el-input v-model="adornProcessForm.bg_color"></el-input>
        </el-form-item>
        <el-form-item label="备注" required prop="notes">
          <el-input type="textarea" v-model="adornProcessForm.notes"></el-input>
        </el-form-item>
        <el-form-item label="描述" required prop="describe">
          <el-input type="textarea" style="height: 200px;" v-model="adornProcessForm.describe"></el-input>
        </el-form-item>
        <el-form-item label="图片" required prop="thumbnail">
          <template>
            <!-- 图片上传  :file-list='adornProcessForm.thumbnail_arr' -->
            <el-upload class="upload-demo" accept="image/*" :data="uploadParams.uploadData" :action="uploadParams.uploadFileUrl"
              :headers="uploadParams.uploadHeaders" list-type="picture" :on-success="uploadSuccess" :file-list='adornProcessForm.thumbnail_arr'
              :on-preview="uploadPreview" :on-remove="uploadRemove" :limit="1" :on-exceed="handleExceed">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </template>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button v-if="operate === 'add'" @click="resetAdornProcessForm">重置</el-button>
        <el-button type="primary" @click="updateOrCreateAdornProcessForm">确认</el-button>
      </span>
    </el-dialog>
    <!-- 表单 end -->
    <!-- 图片预览 -->
    <el-dialog title="图片预览" :visible.sync="imagePreviewVisible" width="50%">
      <div class="demo-image">
        <div class="block">
          <el-image style="width: 100%; height: 100%" :src="previewPath" fit="fill"></el-image>
        </div>
      </div>
    </el-dialog>
    <!-- 关联文章 -->
    <el-dialog title="关联文章" :visible.sync="relatedArticlesVisible" width="80%" center>
      <div style="text-align: center">
        <el-transfer style="text-align: left; display: inline-block" v-model="relatedValue" filterable :titles="['Source', 'Target']"
          :format="{
          noChecked: '${total}', hasChecked: '${checked}/${total}'}" :data="relatedArticleData">
          <span slot-scope="{ option }">{{ option.label }}</span>
        </el-transfer>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRelatedArticle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getAdornProcessListApi,
    deleteAdornProcessApi,
    updateOrCreateAdornProcessApi,
    getUploadParamsApi,
    adornProRelectArticleApi,
    getRelateArticleApi
  } from '../request/api.js'
  export default {
    data() {
      return {
        adornProcessDataList: [],
        totalResult: 0,
        queryParam: {
          title: '',
          currentPage: 1,
          pageSize: 10
        },
        // form操作
        operate: 'add',
        adornProcessFormVisible: false,
        adornProcessForm: {},
        adornProcessFormTem: {
          title: '',
          icon: '',
          notes: '',
          describe: '',
          thumbnail: ''
        },
        adornProcessFormRules: [],
        // 文件上传操作
        uploadParams: {},
        // 预览图片
        imagePreviewVisible: false,
        previewPath: '',
        // 关联文章
        relatedArticlesVisible: false,
        relatedAdornProcessId: null,
        relatedArticleData: [],
        relatedValue: []
      }
    },
    created() {
      this.getAdornProcessList()
      // 浅拷贝值
      this.adornProcessForm = JSON.parse(JSON.stringify(this.adornProcessFormTem))
      // 获取文件上传的参数配置
      this.uploadParams = getUploadParamsApi('adorn_process')
    },
    methods: {
      // 获取列表数据
      async getAdornProcessList() {
        // 获取分类列表
        const {
          data: res
        } = await getAdornProcessListApi(this.queryParam)
        console.log(res)
        this.adornProcessDataList = res.data.dataList
        this.totalResult = res.data.totalResult
      },
      // 分页事件
      handleSizeChange(newSize) {
        this.queryParam.pageSize = newSize
        this.queryParam.currentPage = 1
        this.getAdornProcessList()
      },
      handleCurrentChange(newPage) {
        this.queryParam.currentPage = newPage
        this.getAdornProcessList()
      },
      async deleteAdornProcess(index, id) {
        // 删除
        const data = await deleteAdornProcessApi(id)
        if (data.status === 200 && data.data.status) {
          this.$message.success('删除成功！')
          this.adornProcessDataList.splice(index, 1)
        }
      },
      editAdornProcess(adornProcess) {
        this.adornProcessForm = adornProcess
        this.operate = 'edit'
        this.adornProcessFormVisible = true
      },
      updateOrCreateAdornProcessForm() {
        this.$refs.adornProcessFormRef.validate(async valid => {
          if (valid) {
            const {
              data: res
            } = await updateOrCreateAdornProcessApi(this.adornProcessForm, this.operate)
            console.log(res)
            if (res.status) {
              var message = this.operate === 'add' ? '添加成功！' : '编辑成功！'
              this.$message.success(message)
              this.adornProcessFormVisible = false
              this.operate = 'add'
              this.getAdornProcessList()
            } else {
              this.$message.error('操作失败！')
            }
          }
        })
      },
      // 重置表单
      resetAdornProcessForm() {
        this.adornProcessForm = JSON.parse(JSON.stringify(this.adornProcessFormTem))
        this.$refs.adornProcessFormRef.resetFields()
      },
      // 关闭表单
      closeAdornProcessForm() {
        this.resetAdornProcessForm()
        this.adornProcessFormVisible = false
      },
      // 文件上传成功回调
      uploadSuccess(response) {
        if (response.status) {
          this.adornProcessForm.thumbnail = response.data.id
        }
      },
      // 移除文件
      uploadRemove(file) {
        this.adornProcessForm.thumbnail = null
      },
      // 预览图片
      uploadPreview(file) {
        this.imagePreviewVisible = true
        this.previewPath = file.url
      },
      // 文件超出个数限制时的钩子
      handleExceed(files, fileList) {
        this.$message.warning('只允许上传一图片，请删除后重新上传')
      },
      // 获取关联文章
      async relateArticle(adornProcess) {
        this.relatedValue = adornProcess.relate_article_arr
        console.log(this.relatedValue)
        this.relatedAdornProcessId = adornProcess.id
        const {
          data: res
        } = await getRelateArticleApi()
        this.relatedArticleData = res.data.dataList
        console.log(this.relatedArticleData)
        this.relatedArticlesVisible = true
      },
      // 确认关联
      async submitRelatedArticle() {
        console.log(this.relatedValue)
        const {
          data: res
        } = await adornProRelectArticleApi(this.relatedAdornProcessId, this.relatedValue)
        if (res.status) {
          this.relatedArticlesVisible = false
           this.getAdornProcessList()
          this.$message.success('关联成功')
        } else {
          this.$message.success('关联失败')
        }
      }
    }
  }
</script>

<style>
</style>
